package org.hq.sesponse;

import lombok.Data;

import java.io.Serializable;
/**
 * 返回结果给客户端
 * @param <T>
 */
@Data
public class R<T> implements Serializable {
    //正常返回
    private static final Integer DEFFAULT_SUCCESS_CODE = 200;
    //失败返回
    private static final Integer DEFFAULT_FAILED_CODE = 500;
    //成功返回信息
    private static final String DEFFAULT_SUCCESS_MSG = "成功";
    private static final String DEFFAULT_FAILED_MSG = "失败";
    private Integer code;//状态码
    private String msg;//状态码对应信息
    private T data;//要返回的数据

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> error(T data) {
        return new R<>(DEFFAULT_FAILED_CODE,DEFFAULT_FAILED_MSG,data);
    }
    public static <T> R<T> error() {
        return error(null);
    }
    public static <T> R<T> ok(T data) {
        return new R<>(DEFFAULT_SUCCESS_CODE,DEFFAULT_SUCCESS_MSG,data);
    }
    public static <T> R<T> ok() {
        return ok(null);
    }
}
